<template>
  <div class="login_container inner-container">
    <vue-particles color="#fff" :particleOpacity="0.7" :particlesNumber="60" shapeType="circle" :particleSize="4"
      linesColor="#fff" :linesWidth="1" :lineLinked="true" :lineOpacity="0.4" :linesDistance="150" :moveSpeed="2"
      :hoverEffect="true" hoverMode="grab" :clickEffect="true" clickMode="push">
    </vue-particles>
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img
          src="https://s1.4sai.com/src/img/gif/07/079babd9dbd74f4e88d49c2556bf0ac8.gif?e=1735488000&token=1srnZGLKZ0Aqlz6dk7yF4SkiYf4eP-YrEOdM1sob:GjgtdxTd76jNS06uMt8jGEL_Oik="
          alt="" />
      </div>
      <!-- 登录表单 -->
      <el-form :model="loginForm" :rules="rules" label-width="80px" class="login_form" ref="LoginFormRef">
        <el-form-item prop="username" label="用户名">
          <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>
        <el-form-item prop="password" label="密码">
          <el-input v-model="loginForm.password" prefix-icon="iconfont icon-3702mima"></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="check_code">
          <div class="btns">
            <el-input v-model="loginForm.check_code" prefix-icon="iconfont icon-user" style="width: 100px"></el-input>
            <el-image style="width: 150px; height: 40px;" :src="checkImg.b64s" fit="contain"
              @click="getCheckUrl"></el-image>
          </div>
        </el-form-item>
        <el-form-item class="btns_row">
          <el-button type="primary" round style="border: 1px solid #eee" class="login_container"
            @click="login()">登录</el-button>
          <el-button type="info" round style="background-color: #ddd; border: 1px solid #eee"
            @click="resetForm">重置</el-button>
          <el-button type="info" round style="background-color: #49E6FE; border: 1px solid #eee"
            @click="toRegister">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
//导入登录接口模块
import axios from "axios";

export default {
  name: "loin",
  data() {
    return {
      // 表单数据对象
      loginForm: {
        username: "",
        password: "",
        check_code: "",
        search_id: "",
      },
      // 表单数据验证规则
      rules: {
        username: [
          { required: true, message: "╭(°A°`)╮ 请输入用户名:", trigger: "blur" },
          {
            min: 2,
            max: 25,
            message: "长度在 2 到 25 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "→_→ 请输入密码:", trigger: "blur" },
          { min: 6, max: 15, message: "长度在 6到 15 个字符", trigger: "blur" },
        ],
      },
      //验证码
      checkImg: {
        b64s: "",
        search_id: "",
      }
    };
  },
  methods: {
    //登录方法 再次校验-------------------------------
    login() {
      let that = this
      this.$refs.LoginFormRef.validate(async (result) => {
        if (!result) return;
        const loginData = { ...this.loginForm };
        // console.log(loginData)
        that.$axios.post("/user/login", loginData).then((res) => {
          let result = res.data
          if (result.code === 1000) {
            this.$message({
              message: '登录成功',
              type: 'success'
            });
            localStorage.setItem("token", result.data.token)
            that.$router.push("/index")
            return
          } else {
            this.$message.error(result.msg);
          }
        })
      });
    },
    //添加表单重置方法
    resetForm() {
      this.$refs.LoginFormRef.resetFields();
    },
    //获得验证码
    async getCheckUrl() {
      const resp = await this.$axios.get("/common/checkImg")
      this.checkImg = resp.data.data
      this.loginForm.search_id = this.checkImg.search_id
    },
    toRegister() {
      this.$router.push("/register")
    },
  },
  created() {
    this.getCheckUrl()
  }
};
</script>

<style lang="less" scoped>
.inner-container {
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  overflow-x: hidden;
  overflow-y: hidden;
}

.login_container {
  background-image: -webkit-linear-gradient(left, #1674D6, #2392E0 40%, #38C0F1 75%, #49E6FE 100%);
}

.login_box {
  width: 450px;
  height: 350px;
  background: #fff;
  border-radius: 3px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.avatar_box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
}

.avatar_box img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #eee;
}

.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.btns {
  display: flex;
  justify-content: center;
  background-color: #fff;
}

.btns_row {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  background-color: #fff;
}
</style>
